#include "playbackwidget.h"

PlayBackWidget::PlayBackWidget(QWidget *parent)
    :QWidget (parent)
{
    this->init_window();
    this->init_control();
    this->init_connect();
}

PlayBackWidget::~PlayBackWidget()
{

}

void PlayBackWidget::init_window()
{
    this->setWindowTitle("录像选择界面");
    this->resize(700,700);
}

void PlayBackWidget::init_control()
{
    this->title = new QLabel("录像选择界面", this);
    this->title->move(250,50);
    this->title->setStyleSheet("font-size:36px");
    this->inputDate = new QLabel("请输入日期", this);
    this->inputDate->move(200,125);
    this->inputDate->setStyleSheet("font-size:20px");

    this->calendarBtn = new QPushButton("选择日期", this);
    this->calendarBtn->move(350,115);
    this->calendarBtn->setStyleSheet("width:200px");

    this->enterBtn = new QPushButton("确定", this);
    this->enterBtn->move(150,600);
    this->enterBtn->setStyleSheet("height:30px;width:40px");
    this->backBtn = new QPushButton("返回", this);
    this->backBtn->move(450,600);
    this->backBtn->setStyleSheet("height:30px;width:40px");
//    this->prePageBtn = new QPushButton("上一页", this);
//    this->prePageBtn->move(150,525);
//    this->prePageBtn->setStyleSheet("height:30px;width:40px");
//    this->nextPageBtn = new QPushButton("下一页", this);
//    this->nextPageBtn->move(450,525);
//    this->nextPageBtn->setStyleSheet("height:30px;width:40px");


    this->listwidget = new QListWidget(this);
    this->listwidget->move(100,175);
    this->listwidget->resize(500,300);

    this->calendar = new QCalendarWidget(this);
    this->calendar->move(250,150);
    this->calendar->raise();
    this->calendar->hide();

    //获取该用户的视频
    char **qres=nullptr;
    int col = 0,row=0;
    int res = VideoModel::getInstance()->selectVideo(qres, row, col);
    if(res==0){
        for (int i=1;i<=row;i++) {
            qDebug() << i;

            this->listwidgetItem = new QListWidgetItem(QIcon(qres[i*col+1]), qres[i*col+2]);
            this->listwidgetItem->setData(Qt::UserRole, QString(qres[i*col]));
            qDebug() <<  this->listwidgetItem->data(Qt::UserRole).toString();
            this->listwidget->addItem(this->listwidgetItem);
        }
        this->listwidget->setViewMode(QListView::IconMode);
        this->listwidget->setIconSize(QSize(200,200));
        this->listwidget->setResizeMode(QListWidget::Adjust);
        this->listwidget->setMovement(QListWidget::Static);
    }

}

void PlayBackWidget::init_connect()
{
    connect(this->calendarBtn, SIGNAL(clicked()), this, SLOT(showCalendar()));
    connect(this->calendar, SIGNAL(selectionChanged()), this, SLOT(on_calendarWidget_selectionChanged()));
    connect(this->listwidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(showVideo(QListWidgetItem*)));
//    connect(this->record, SIGNAL(backToPalyWidget()), this, SLOT(showPlay()));
    connect(this->backBtn, SIGNAL(clicked()), this, SIGNAL(backToMain()));

}

void PlayBackWidget::showCalendar()
{
    this->calendar->show();
}

void PlayBackWidget::on_calendarWidget_selectionChanged()
{
    QString ss = this->calendar->selectedDate().toString("yyyy-MM-dd");
    this->calendarBtn->setText(this->calendar->selectedDate().toString());
    this->calendar->hide();

    this->listwidget->clear();
    //查询日期

//    //获取该用户的照片
    char **qres=nullptr;
    int col = 0,row=0;
    int res = VideoModel::getInstance()->selectVideo(qres, row, col);
    if(res==0){
        for (int i=1;i<=row;i++) {
            //如果是这个日期
            qDebug() << qres[i*col+2];
            if(QString(qres[i*col+2]).contains(ss))
            {
                this->listwidgetItem = new QListWidgetItem(QIcon(qres[i*col+1]), qres[i*col+2]);
                this->listwidgetItem->setData(Qt::UserRole, QString(qres[i*col]));
                qDebug() <<  this->listwidgetItem->data(Qt::UserRole).toString();
                this->listwidget->addItem(this->listwidgetItem);
            }

        }
        this->listwidget->setViewMode(QListView::IconMode);
        this->listwidget->setIconSize(QSize(200,200));
        this->listwidget->setResizeMode(QListWidget::Adjust);
        this->listwidget->setMovement(QListWidget::Static);
    }
}

void PlayBackWidget::showVideo(QListWidgetItem *video)
{
    QString video_cover = video->data(Qt::UserRole).toString();

    qDebug() << video_cover;
//    qDebug() << video->data(Qt::UserRole+1).toString();

    for(int i=0; i<this->listwidget->count(); i++){
        this->videoList.append(this->listwidget->item(i)->data(Qt::UserRole).toString());
    }
    this->record = new RecordWidget(this->videoList, video_cover);

    connect(this->record, SIGNAL(backToPalyWidget()), this, SLOT(showPlay()));
    this->record->show();
}

void PlayBackWidget::showPlay()
{
    this->record->close();
    this->show();
}

void PlayBackWidget::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
                tr("Are you sure you want to exit?\n"),
                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                QMessageBox::Yes);

    if (resBtn != QMessageBox::Yes) {
        event->ignore();
    } else {
//        UserModel::getInstance()->updateUserAll();
        event->accept();
    }
}
